package com.danbay.cloud.project.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.danbay.cloud.project.api.dto.*;
import com.danbay.cloud.project.api.service.IHouseService;
import com.danbay.cloud.project.entity.House;
import com.danbay.cloud.project.entity.Room;
import com.danbay.cloud.project.mapper.HouseMapper;
import com.danbay.cloud.project.mapper.RoomMapper;
import com.danbay.framework.db.pagination.PageParam;
import com.danbay.framework.db.pagination.PageQuery;
import com.danbay.framework.db.pagination.PageResult;
import com.danbay.framework.db.pagination.PagingHelper;
import com.danbay.framework.exception.BusinessException;
import com.danbay.framework.utils.BeanUtils;
import com.danbay.framework.utils.BeanValidator;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author SJG
 *         2018/1/29.
 */
@Service
public class HouseService implements IHouseService {
    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private RoomMapper roomMapper;

    /**
     * 添加房源
     *
     * @param house 房源信息
     * @throws BusinessException 业务异常
     */
    @Override
    public void add(HouseDto house) throws BusinessException {
        BeanValidator.valid(house);
        House h = BeanUtils.convert(house, House.class);
        h.setAddTime(DateTime.now().toDate());
        if (houseMapper.selectById(house.getId()) != null) {
            throw new BusinessException("房源id已存在");
        }
        houseMapper.insert(h);
    }

    /**
     * 修改房源
     *
     * @param house 房源信息
     * @throws BusinessException 业务异常
     */
    @Override
    public void edit(HouseDto house) throws BusinessException {
        BeanValidator.valid(house);
        houseMapper.updateById(BeanUtils.convert(house, House.class));
    }

    /**
     * 删除房源
     *
     * @param hid 房源ID
     * @throws BusinessException 业务异常
     */
    @Override
    public void del(String hid) throws BusinessException {
        House house = houseMapper.selectById(hid);
        if (house == null) {
            throw new BusinessException("该房间不存在");
        }
        if (roomMapper.selectCount(new EntityWrapper<>(Room.builder().houseId(hid).build())) > 0) {
            throw new BusinessException("该房源下已经添加了房间，不能删除");
        }
        houseMapper.deleteById(hid);
    }

    /**
     * 分页查询房源信息
     *
     * @param param 查询参数
     * @return 房源列表
     */
    @Override
    public PageResult<HouseInfoDto> findHouses(PageParam<HouseQueryDto> param) {
        return PagingHelper.pageQuery(param, HouseInfoDto.class, House.class,
                (PageQuery<House, House>) (page, param1) -> houseMapper.findHouses(page, param1));
    }

    /**
     * 通过房源id列表批量取房源信息
     *
     * @param houseIds 房源id列表
     * @return 房源信息列表
     */
    @Override
    public List<HouseInfoDto> getHouses(List<String> houseIds) {
        return BeanUtils.convert(houseMapper.getHousesByIds(houseIds), HouseInfoDto.class);
    }

    /**
     * 使用magicId查询房源名列表
     *
     * @param magicId 房源id前面部分
     * @return 房源名列表
     */
    @Override
    public List<HouseNameDto> findHouseNames(String magicId) {
        return BeanUtils.convert(houseMapper.findHouseNames(House.builder().id(magicId).build()), HouseNameDto.class);
    }

    /**
     * 使用房源id取得该房源所有楼层列表
     *
     * @param houseId 房源id
     * @return 楼层列表
     */
    @Override
    public List<String> getHouseFloors(String houseId) {
        return houseMapper.getHouseFloors(houseId);
    }

    /**
     * 添加房间
     *
     * @param roomDto 房间信息
     * @throws BusinessException 业务异常
     */
    @Override
    public void addRoom(RoomDto roomDto) throws BusinessException {
        BeanValidator.valid(roomDto);
        Room room=BeanUtils.convert(roomDto,Room.class);
        room.setAddTime(DateTime.now().toDate());
        room.setDel(false);
        room.setRented(false);
        roomMapper.insert(room);
    }

    /**
     * 修改房间
     *
     * @param room 房间信息
     * @throws BusinessException 业务异常
     */
    @Override
    public void editRoom(RoomDto room) throws BusinessException {
        BeanValidator.valid(room);
        roomMapper.updateById(BeanUtils.convert(room,Room.class));
    }

    /**
     * 删除房间
     *
     * @param rid 房间id
     * @throws BusinessException 业务异常
     */
    @Override
    public void delRoom(String rid) throws BusinessException {

    }

    /**
     * 分页查询房间信息
     *
     * @param param 查询参数
     * @return 房间列表
     */
    @Override
    public PageResult<RoomDto> findRoom(PageParam<RoomQueryDto> param) {
        return PagingHelper.pageQuery(param, RoomDto.class, Room.class,
                (PageQuery<Room, Room>) (page, param1) -> roomMapper.findRooms(page, param1));
    }
}
